﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Collections;
using GSLMS.Entity.Enum;

namespace GSLMS.Core
{
    public class Loger
    {
        #region 日志记录


        public void ExceptionHand(Exception e)
        {
            string title = this.ToString() + e.TargetSite;
            string message = e.Source + "_" + e.Message + Environment.NewLine + e.StackTrace;
            WriteGeneralLog(title, message, Priority.Normal);
#if DEBUG
            throw e;
#endif
        }

        /// <summary>
        /// 异常错误日志记录
        /// </summary>
        /// <param name="p_Title">标题</param>
        /// <param name="p_Message">内容</param>
        /// <param name="p_Priority">级别</param>
        /// <returns></returns>
        public void WriteGeneralLog(string p_Title, string p_Message, Priority p_Priority)
        {
            try
            {
                this.WriteLog(p_Title, p_Message, p_Priority, Category.General);
            }
            catch (Exception e)
            {
#if DEBUG
                throw e;
                #endif
            }
        }

        /// <summary>
        /// 跟踪日志记录
        /// </summary>
        /// <param name="p_Title">标题</param>
        /// <param name="p_Message">内容</param>
        /// <param name="p_Priority">级别</param>
        /// <returns></returns>
        public void WriteTraceLog(string p_Title, string p_Message, Priority p_Priority)
        {
            try
            {
                this.WriteLog(p_Title, p_Message, p_Priority, Category.Trace);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private void WriteLog(string p_Title, string p_Message, Priority p_Priority, string p_Category)
        {
            try
            {
                int priority = (int)p_Priority;
                string category = p_Category.ToString();

                LogEntry logEntry = new LogEntry();

                logEntry.TimeStamp = DateTime.Now;
                logEntry.Title = p_Title;
                logEntry.Message = p_Message;
                logEntry.Priority = priority;
                logEntry.Categories.Add(category);

                Logger.Write(logEntry, category);
            }
            catch (Exception e)
            {
#if DEBUG
                throw e;
#endif
            }
        }
        #endregion
    }
}
